package net.example.lesson4;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class TestList {

	static final int N = 50000;

	static long addOrderedList(List list) {
		long start = System.currentTimeMillis();
		Object o = new Object();
		for (int i = 0; i < N; i++) {
			list.add(0, o);
		}
		return System.currentTimeMillis() - start;
	}

	static long addTailList(List list) {
		long start = System.currentTimeMillis();
		Object o = new Object();
		for (int i = 0; i < N; i++) {
			list.add(o);
		}
		return System.currentTimeMillis() - start;
	}

	static long readList2(List list) {
		long start = System.currentTimeMillis();
     	for (int i = 0; i < list.size(); i++) {
			list.get(i);
		}
		return System.currentTimeMillis() - start;
	}
	
	static long readList(List list) {
		long start = System.currentTimeMillis();
		// for (int i = 0; i < list.size(); i++) {
		for (int i = 0, j = list.size(); i < j; i++) {
			list.get(i);
		}
		return System.currentTimeMillis() - start;
	}

	static long removeList(List list) {
		long start = System.currentTimeMillis();
		for (int i = 0, j = list.size(); i < j; i++) {
			list.remove(0);
		}
		return System.currentTimeMillis() - start;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final List arrayList = new ArrayList();
		final List linkedList = new LinkedList();
		System.out.println("ArrayList添加" + N + "条耗时：" + addOrderedList(arrayList));
		System.out.println("LinkedList添加" + N + "条耗时：" + addOrderedList(linkedList));
		System.out.println("==========================================");

		
		System.out.println("ArrayList末尾添加" + N + "条耗时：" + addTailList(arrayList));
		System.out.println("LinkedList末尾添加添加" + N + "条耗时：" + addTailList(linkedList));
		System.out.println("==========================================");
		
		
		System.out.println("ArrayList查找" + N + "条耗时：" + readList(arrayList));
		System.out.println("LinkedList查找" + N + "条耗时：" + readList(linkedList));
		System.out.println("==========================================");
		
		System.out.println("ArrayList 优化查找" + N + "条耗时：" + readList2(arrayList));
		System.out.println("LinkedList 优化查找" + N + "条耗时：" + readList2(linkedList));
		System.out.println("==========================================");
		
		
		System.out.println("ArrayList删除" + N + "条耗时：" + removeList(arrayList));
		System.out.println("LinkedList删除" + N + "条耗时：" + removeList(linkedList));

	}
}
